home *** CD-ROM | disk | FTP | other *** search
- Path: crchh327.rich.bnr.ca!jobell
- From: jobell@bnr.ca (Bret Bieghler)
- Newsgroups: comp.lang.c++
- Subject: Re: Creating an object via new with ONLY a pointer to the object
- Date: 11 Apr 1996 15:56:24 GMT
- Organization: Bell-Northern Research Ltd.
- Message-ID: <4kja38$d46@crchh327.rich.bnr.ca>
- References: <4kh07v$lno@crchh327.rich.bnr.ca> <4kifrt$lk1@ubszh.fh.zh.ubs.com>
- NNTP-Posting-Host: crchhd4a.rich.bnr.ca
-
- In article <4kifrt$lk1@ubszh.fh.zh.ubs.com>,
- Ian Johnston (by ubsswop) <ian.johnston@ubs.com> wrote:
- >In article <4kh07v$lno@crchh327.rich.bnr.ca>, jobell@bnr.ca (Bret Bieghler) writes:
- >|> An interesting problem I've come across... I was wondering if this
- >|> is possible:
- >|>
- [deleted]
- >
- >Give each CommadObject class a *static* function:
- >
- >class ExitCommand : public CommandObject
- >{
- > // ...
- > static CommandObject *makeNew();
- >};
- >
- >CommandObject *ExitCommand::makeNew()
- >{
- > return new ExitCommand;
- >}
- >
- >
- >Now you can make a table (assoc array, whatever) of strings and pointers
- >to these functions.
- >
- >typedef CommandObject *(*MakeCmdObj)();
- >
- >MakeCmdObj funcptr = commandTable[command];
- >CommandObject *baseptr = (*funcptr)();
- >
- >
- >Ian
-
- Thanks for those who replied. Ian's method has been the most elegant
- solution to the problem. My associative array contains strings and
- function pointers. When I need an object associated with the string
- I get the function pointer and execute it.
-
- Thank you very much Ian.
-
- Regards,
-
- Joseph Bell
-
-
- --
- Joseph A. Bell (NOT Bret Bieghler) jobell@bnr.ca
- Northern Telecom / Bell-Northern Research
- "What? Evacuate now, in our moment of triumph? Surely you overestimate their chances."
-